package com.langsin.zhujiaqi;

import cn.hutool.core.util.IdUtil;
import com.langsin.bean.*;
import com.langsin.zhujiaqi.mapper.TeacherMapper;
import com.langsin.zhujiaqi.mapper.UserMapper;
import com.langsin.zhujiaqi.mapper.WorkerMapper;
import com.langsin.zhujiaqi.mapper.GoodsMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class MyTest {
    SqlSession sqlSession = null;

    @Test
    public void testVO() throws Exception{
        QueryVo vo = new QueryVo();
        vo.setRoleCode("manager");
        vo.setAddress("济南浪曦");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryByRoleAndAddress(vo);
        users.forEach(System.out::println);
    }

    @Test
    public void testMap() throws Exception{
        HashMap<String,Object> map = new HashMap<>();
        map.put("roleCode","plain");
        map.put("address","济南浪曦");
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        List<User> users = userMapper.queryUserByMap(map);
        users.forEach(System.out::println);
    }

    @Test
    public void testUser() throws Exception{
        Integer uid = 702;
        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
        User user = userMapper.queryById(uid);
        System.out.println(user);
    }

    @Test
    public void testTabname() throws Exception{
        String tabName = "worker";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int counts = goodsMapper.counts(tabName);
        System.out.println(tabName + "=" + counts);
    }

    @Test
    public void testMapperLike2() throws Exception{
        String name = "C";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName2(name);
        for (Goods good : goods) {
            System.out.println(good);
        }
    }

    @Test
    public void testMapperLike() throws Exception{
        String name = "%C%";
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        List<Goods> goods = goodsMapper.queryByName(name);
        for (Goods good : goods) {
            System.out.println(good);
        }
    }

    @Test
    public void testMapperGoods() throws Exception {
        Goods goods = new Goods();
        goods.setGoodsName("AOC24寸显示器");
        goods.setShopPrice(1499.00);
        GoodsMapper goodsMapper = sqlSession.getMapper(GoodsMapper.class);
        int i = goodsMapper.addGoods(goods);
        System.out.println(goods);
    }

    @Test
    public void testMapper3() throws Exception {
        Worker worker = new Worker();
        worker.setWname("工程师");
        worker.setAge(27);
        worker.setSex("男");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int i = workerMapper.insertWorker2(worker);
        System.out.println(i + "----->" + worker);
    }

    @Test
    public void testMapper2() throws Exception {
        Worker worker = new Worker();
        worker.setWid(IdUtil.getSnowflakeNextIdStr());
        worker.setWname("设计师");
        worker.setAge(37);
        worker.setSex("男");

        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        int row = workerMapper.insertWorker(worker);
        System.out.println(row);
    }

    @Test
    public void testMapper1() throws Exception {
        WorkerMapper workerMapper = sqlSession.getMapper(WorkerMapper.class);
        List<Worker> workerList = workerMapper.selectByAge(50);
        workerList.forEach(System.out::println);
    }

    @Before
    public void test() throws Exception {
        InputStream stream = Resources.getResourceAsStream("mybatis.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(stream);
        sqlSession = sqlSessionFactory.openSession();
    }

    @After
    public void after() throws Exception {
        sqlSession.commit();
        sqlSession.close();
    }

    @Test
    public void testUpdate() throws Exception {

    }
}
